using Unity.Netcode;

namespace Since.Netcode
{
	public abstract class INetworkWraperLogic<T> : INetworkLogic<T>
		where T : NetworkBehaviour
	{

		private INetworkNonAuthority<T> _NonAuthority;
		internal void SetNonAuthority<R>(R nonAuthority) where R : INetworkNonAuthority<T>
		{
			_NonAuthority = nonAuthority;
		}

		private INetworkAuthority<T> _Authority;
		internal void SetAuthority<W>(W authority) where W : INetworkAuthority<T>
		{
			_Authority = authority;
		}


		#region Unity生命周期
		internal void InnerUpdate()
		{
			_NonAuthority?.Update();

			_Authority?.Update();

			Update();
		}

		internal void InnerOnEnable()
		{
			_NonAuthority?.OnEnable();

			_Authority?.OnEnable();

			OnEnable();
		}

		internal void InnerOnDisable()
		{
			_NonAuthority?.OnDisable();

			_Authority?.OnDisable();

			OnDisable();
		}

		internal void InnerLateUpdate()
		{
			_NonAuthority?.LateUpdate();

			_Authority?.LateUpdate();

			LateUpdate();
		}

		internal void InnerFixedUpdate()
		{
			_NonAuthority?.FixedUpdate();

			_Authority?.FixedUpdate();

			FixedUpdate();
		}

		internal void InnerOnDestroy()
		{
			_NonAuthority?.OnDestroy();
			_Authority?.OnDestroy();

			OnDestroy();
		}

		internal void InnerOnApplicationPause(bool pauseStatus)
		{
			_NonAuthority?.OnApplicationPause(pauseStatus);

			_Authority?.OnApplicationPause(pauseStatus);

			OnApplicationPause(pauseStatus);
		}

		internal void InnerOnApplicationQuit()
		{
			_NonAuthority?.OnApplicationQuit();

			_Authority?.OnApplicationQuit();

			OnApplicationQuit();
		}
		#endregion

		#region 网络生命周期
		internal void InnerOnNetworkSpawn()
		{
			_NonAuthority?.OnNetworkSpawn();

			_Authority?.OnNetworkSpawn();

			OnNetworkSpawn();
		}

		internal void InnerOnNetworkDespawn()
		{
			_NonAuthority?.OnNetworkDespawn();

			_Authority?.OnNetworkDespawn();

			OnNetworkDespawn();
		}

		internal void InnerOnGainedOwnership()
		{
			_NonAuthority?.OnGainedOwnership();

			_Authority?.OnGainedOwnership();

			OnGainedOwnership();
		}

		internal void InnerOnOwnershipChanged(ulong previous, ulong current)
		{
			_NonAuthority?.OnOwnershipChanged(previous, current);

			_Authority?.OnOwnershipChanged(previous, current);

			OnOwnershipChanged(previous, current);
		}

		internal void InnerOnLostOwnership()
		{
			_NonAuthority?.OnLostOwnership();

			_Authority?.OnLostOwnership();

			OnLostOwnership();
		}

		internal void InnerOnNetworkObjectParentChanged(NetworkObject parentNetworkObject)
		{
			_NonAuthority?.OnNetworkObjectParentChanged(parentNetworkObject);

			_Authority?.OnNetworkObjectParentChanged(parentNetworkObject);

			OnNetworkObjectParentChanged(parentNetworkObject);
		}

		internal void InnerOnReanticipate(double lastRoundTripTime)
		{
			_NonAuthority?.OnReanticipate(lastRoundTripTime);

			_Authority?.OnReanticipate(lastRoundTripTime);

			OnReanticipate(lastRoundTripTime);
		}
		#endregion
	}
}

